package com.ssly.weixin.xichang.admin.service.impl;

import com.ssly.weixin.xichang.admin.domain.Admin;
import com.ssly.weixin.xichang.admin.repository.AdminRepository;
import com.ssly.weixin.xichang.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminRepository adminRepository;
    @Override
    public Admin getAdminByName(String username) {
        return adminRepository.findTopByUsername(username).get();
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = getAdminByName(username);
        if (admin == null) {
            throw new UsernameNotFoundException(username);
        }
        boolean booleanStatus = true;
        SimpleGrantedAuthority simpleGrantedAuthority=new SimpleGrantedAuthority("ROLE_ADMIN");
        List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<>();
        grantedAuthorities.add(simpleGrantedAuthority);
        return new User(admin.getUsername(),admin.getPassword(),true,true,true, true,grantedAuthorities);
    }
}
